package com.wyp168.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName LockDemo001
 * @Description TODO
 * @Author wyp168
 * @Date 2022/7/8 12:47
 */
public class LockDemo001 {
    //创建独占锁
    public static final Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        Thread threadA = new Thread(() -> {
            //获取独占锁
            try {
                lock.lock();
                System.out.println("ThreadA get lock");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally{
                //释放独占锁
                lock.unlock();
                System.out.println("ThreadA release lock");
            }
        });

        Thread threadB = new Thread(() -> {
            threadA.interrupt();
            //获取独占锁
            try {
                lock.lock();
                System.out.println("ThreadB get lock");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally{
                //释放独占锁
                lock.unlock();
                System.out.println("ThreadB release lock");
            }
        });


        //设置线程优先级
        threadA.setPriority(9);
        threadB.setPriority(3);

        //启动线程
        threadA.start();
        threadB.start();
    }
}
